/**
 * @author alonsocucei
 * @version 1.0
 */
package modulo1.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class DVDMenu {
    private static Locale currentLocale = Locale.getDefault();
    private static ResourceBundle bundle = ResourceBundle.getBundle("modulo1.locales.MenuBundle", currentLocale);        
    private DVDMenu[] items = new DVDMenu[0];
    private Action[] actions = new Action[0];
    private String title;
    private boolean isRoot;

    /**
     * Constructs a new menu with a title.
     * @param title the name for this menu
     */
    public DVDMenu(String title) {
        this.title = title;
    }

    /**
     * Constructs a new menu with submenus and actions for every submenu
     * @param items an array containing the submenus for this menu
     * @param actions an array containing the actions for every submenu
     */
    public DVDMenu(DVDMenu[] items, Action[] actions) {
        this.items = new DVDMenu[items.length];
        System.arraycopy(items, 0, this.items, 0, items.length);
        this.actions = new Action[actions.length];
        System.arraycopy(actions, 0, this.actions, 0, actions.length);
    }

    /**
     * Sets an array with submenus for this menu.
     * This method is used for in cases where the menu changes dynamically
     * according to the state of the applications or if the constructor
     * used to create a menu was <code>DVDMenu(String)</code>. 
     * @param items the array with the submenus
     */
    public void setItems(DVDMenu[] items) {
        this.items = new DVDMenu[items.length];
        System.arraycopy(items, 0, this.items, 0, items.length);
    }

    /**
     * Sets an array with actions which correspond one for submenu.
     * This method is used for in cases where the menu changes dynamically
     * according to the state of the applications or if the constructor
     * used to create a menu was <code>DVDMenu(String)</code>. 
     * @param actions the array with the actions for the submenus
     */
    public void setActions(Action[] actions) {
        this.actions = new Action[actions.length];
        System.arraycopy(actions, 0, this.actions, 0, actions.length);
    }

    /**
     * Returns the length of all items for this menu.
     * The length is equals to the items length + actions length.
     * @return the number of elements (submenus + actions) for this menu
     */
    public int getItemsLength() {
        return items.length + actions.length + (isRoot() ? 1 : 2);
    }

    /**
     * Sets to <code>true</code> or <code>false</code> if this is the main menu for the application.
     * @param isRoot a <code>boolean</code> value to indicate if this is the main menu
     */
    public void setRoot(boolean isRoot) {
        this.isRoot = isRoot;
    }

    /**
     * Returns a 
     * @return <code>true</code> if this menu is not a submenu of another menu, other way <code>false</code>
     */
    public boolean isRoot() {
        return isRoot;
    }

    /**
     * Returns all the submenus and actions for this menu (including itself)
     * @return an array containing all items (submenus and actions) for this menu
     */
    public Object[] getItems() {
        Object[] items = new Object[getItemsLength() + (isRoot() ? 1 : 2)];
        System.arraycopy(this.items, 0, items, 0, this.items.length);
        System.arraycopy(this.actions, 0, items, this.items.length, this.actions.length);
        
        if (!isRoot()) {
            items[items.length - 2] = bundle.getString("backToPreviousMenu");
        }
        
        items[items.length - 1] = bundle.getString("exit");
        
        return items;
    }

    /**
     * Sets the locale for this menu
     * @param locale the locale for this menu
     */
    public static void setLocale(Locale locale) {
        currentLocale = locale;
        bundle = ResourceBundle.getBundle("modulo1.locales.MenuBundle", currentLocale);        
    }
    
    /**
     * Returns the representation of this menu which is ideal to display in a console.
     * @return a <code>String</code> formatted as a menu ready to display in a console
     */
    @Override
    public String toString() {
        StringBuilder menuString = new StringBuilder();

        int menuNumber = 0;

        for (DVDMenu item : items) {
            menuString.append(++menuNumber);
            menuString.append(".-");
            menuString.append(bundle.getString(item.title));
            menuString.append("-> \r\n");
        }

        for (Action action : actions) {
            menuString.append(++menuNumber);
            menuString.append(".-");
            menuString.append(bundle.getString(action.getTitle()));
            menuString.append("\r\n");
        }

        if (!isRoot()) {
            menuString.append(++menuNumber);
            menuString.append(".-");
            menuString.append(bundle.getString("backToPreviousMenu"));
            menuString.append("\r\n");
        }

        menuString.append(++menuNumber);
        menuString.append(".-");
        menuString.append(bundle.getString("exit"));

        return menuString.toString();
    }
}